<?php

/**
 * 逛宝贝页面
 */
class bookAction extends frontendAction {
   
    public $totalid = array();
    public function _initialize() {
        parent::_initialize();
		
        $this->assign('nav_curr', 'book');
    }

    /**
     * 逛宝贝首页
     */
    public function index() {
        $sort = $this->_get('sort', 'intval'); //排序

        //$where = array();
        //排序：最热(hot)，最新(new)
        switch ($sort) {
            case 'hot':
                $order = 'id DESC';
                $where = array('hot'=>1);
                break;
            case 'new':
                $order = 'id DESC';
				$where = array('new'=>1);
                break;
			default:
				$this->_404();
				break;
        }
		
		$goods = M('goods');
		$lst = $goods->where($where)->order('orderby DESC, id DESC')->select();
		foreach($lst as $key=>$v){
			$tmp0 = explode('|',$v['wudu']);
			$tmp1 = array();
			foreach($tmp0 as $v){
				$tmp1[] = array('wd'=>$v);
			}
			$lst[$key]['wudu'] = $tmp1;
		}

		
        $this->assign('havedata', $lst?'y':'n');
        $this->assign('item_list', $lst);
        $this->assign('sort', $sort);
        $this->_config_seo(C('pin_seo_config.book'), array('tag_name' => $tag)); //SEO
        $this->display();
    }

    public function index_ajax() {
        $tag = $this->_get('tag', 'trim'); //标签
        $sort = $this->_get('sort', 'trim', 'hot'); //排序
        switch ($sort) {
            case 'hot':
                $order = 'hits DESC,id DESC';
                break;
            case 'new':
                $order = 'id DESC';
                break;
        }
        $where = array();
        $tag && $where['intro'] = array('like', '%' . $tag . '%');
        $this->wall_ajax($where, $order);
    }

	/*
	 *
	*/
	public function select() {
        //一键下单选项
        $selects = array(
            'touguang_level' => '透光性',
            // 'simidu' => '私密度',
            'gere_level' => '隔热率',
            'xiaoshoujia' => '销售价'
            // 'jianglie' => '奖励额',
        );

        $this->assign('selects', $selects);
		$this->display();
	}
	
	private function chackI($str, $min, $max, $def){
		if($str){
			if($str>=$min && $str<=$max){
				return $str;
			}
		}
		return $def;
	}

    public function cate_type($pid=0){
        $Model = M('item_cate');
        $list = $Model->query(" SELECT id,pid FROM wxs_item_cate WHERE pid=".$pid);
        if (!empty($list)) {
            foreach ($list as $key => $value) {
                $this->totalid[] = $value['id'];
                $this->cate_type($value['id']);
            }
        }
    }
    

    public function cate() {
        $where = "status=1";

        //筛选参数
        $filters = array('simidu', 'xiaoshoujia', 'jianglie', 'gere_level', 'touguang_level');
        foreach($filters as $f) {
            if(isset($_GET[$f])) {
                $val = $_GET[$f];
                if($val == '0' || empty($val)) {
                    //do nothing
                } elseif (strpos($val, ',')) {
                    $lr = explode(',', $val);
                    if(count($lr) == 2) {
                        $where .= " and $f between $lr[0] and $lr[1]";
                    }
                } else {
                    $where .= " and $f=$val";
                }
            }
        }


        //分类
        if(!empty($_GET['cid']) ) {
            $this->cate_type($_GET['cid']);
            if (!empty($this->totalid)) {
                $tid = implode(',',$this->totalid) . ',' . $_GET['cid'] ;
                $where .= " and cid in ( {$tid} )";
            }else{
                 $where .= " and cid=" . $_GET['cid'];
            }
        }

        //关键词
        $keyword = $this->_get('key', 'trim');
        if($keyword) {
            $where .= " and ( title like \"%$keyword%\" or fbt like \"%$keyword%\")";
        }

        //查询
        $goods = M('goods')->where($where);

        if(isset($_GET['numonly']) && $_GET['numonly'] == '1') {
            echo $goods->count();exit;
        }

        $lst = $goods->order('id DESC')->select();

        $this->assign('item_list', $lst);
        $this->assign('havedata', count($lst) > 0 ? 'y':'n');

        $show_nav = $this->_get('sn', 'intval', 1);	//是否显示菜单
        $this->assign('show_nav',$show_nav);

        $this->display();
    }

    /**
     * 按分类查看
     */
    public function cate2() {
		$keyword = $this->_get('key', 'trim');	//搜索关键字
		$qx = $this->chackI($this->_get('qx', 'intval', 3), 1, 3, 3);
		$gr = $this->chackI($this->_get('gr', 'intval', 3), 1, 3, 3);
		$tg = $this->chackI($this->_get('tg', 'intval', 3), 1, 3, 3);
		$sm = $this->chackI($this->_get('sm', 'intval', 3), 1, 3, 3);
		$sj = $this->chackI($this->_get('sj', 'intval', 3), 1, 3, 3);
		$jl = $this->chackI($this->_get('jl', 'intval', 3), 1, 3, 3);
		
		$show_nav = $this->_get('sn', 'intval', 1);	//是否显示菜单
		
		$show_nav = ($keyword!=''?0:$show_nav);
        $cid = $this->_get('cid', 'intval', 0);
        //!$cid && $this->_404();
		
        //分类数据
        if (false === $cate_data = F('cate_data')) {
            $cate_data = D('item_cate')->cate_data_cache();
        }
        //当前分类信息
        if (isset($cate_data[$cid])) {
            $cate_info = $cate_data[$cid];
        } else {
            //$this->_404();
			foreach($cate_data as $key => $value){
				if($value['pid']!='0'){
					$cid=$key;
					break;
				}
			}
			$cate_info = $cate_data[$cid];
        }
        //分类列表
        if (false === $cate_list = F('cate_list')) {
            $cate_list = D('item_cate')->cate_cache();
        }
        //分类关系
        if (false === $cate_relate = F('cate_relate')) {
            $cate_relate = D('item_cate')->relate_cache();
        }
        //获取当前分类的顶级分类
        $tid = $cate_relate[$cid]['tid'];
		
        //条件
        $where = array();
		if($keyword){
			$where['title']=array('like',"%$keyword%");
		}else{
			$where['cid']=array('eq',$cid);
			$qx_min = (($qx-1)/3)*100;
			$qx_max = (($qx/3)*100);
			$where['qingxidu']=array('between',"$qx_min, $qx_max");
			$gr_min = (($gr-1)/3)*100;
			$gr_max = (($gr/3)*100);
			$where['geredu']=array('between',"$gr_min, $gr_max");
			$tg_min = (($tg-1)/3)*100;
			$tg_max = (($tg/3)*100);
			$where['touguangdu']=array('between',"$tg_min, $tg_max");
			$sm_min = (($sm-1)/3)*100;
			$sm_max = (($sm/3)*100);
			$where['simidu']=array('between',"$sm_min, $sm_max");
			$sj_min = (($sj-1)/3)*100;
			$sj_max = (($sj/3)*100);
			$where['xiaoshoujia']=array('between',"$sj_min, $sj_max");
			$jl_min = (($jl-1)/3)*100;
			$jl_max = (($jl/3)*100);
			$where['jianglie']=array('between',"$jl_min, $jl_max");
		}
		$where['status']=array('eq',1);
		
		$goods = M('goods');
		$lst = $goods->where($where)->order('orderby DESC, id DESC')->select();
		foreach($lst as $key=>$v){
			$tmp0 = explode('|',$v['wudu']);
			$tmp1 = array();
			foreach($tmp0 as $v){
				$tmp1[] = array('wd'=>$v);
			}
			$lst[$key]['wudu'] = $tmp1;
		}

		$this->assign('havedata', $lst?'y':'n');
        $this->assign('item_list', $lst);
		
		$this->assign('show_nav',$show_nav);	//是否显示菜单 0：不显示 1：显示
		$this->assign('cid',$cid);	//当前分类ID
		$this->assign('tid',$tid);	//当前父分类ID
        $this->assign('cate_list', $cate_list); //分类
        $this->assign('tid', $tid); //顶级分类ID
        $this->assign('cate_info', $cate_info); //当前分类信息
        $this->assign('sort', $sort); //排序
        $this->assign('nav_curr', 'cate'); //导航设置
        //SEO
        $this->_config_seo(C('pin_seo_config.cate'), array(
            'cate_name' => $cate_info['name'],
            'seo_title' => $cate_info['seo_title'],
            'seo_keywords' => $cate_info['seo_keys'],
            'seo_description' => $cate_info['seo_desc'],
        ));
        $this->display();
    }

    /**
     * 标签分类瀑布
     */
    public function tcate_waterfall($where, $order = 'i.id DESC', $field = '') {
        $db_pre = C('DB_PREFIX');
        $item_tag_table = $db_pre . 'item_tag';
        $item_tag_mod = M('item_tag');
        $where_init = array('i.status' => '1');
        $where = array_merge($where_init, $where);
        $count = $item_tag_mod->where($where)->join($db_pre . 'item i ON i.id = ' . $item_tag_table . '.item_id')->count();
        $spage_size = C('pin_wall_spage_size'); //每次加载个数
        $spage_max = C('pin_wall_spage_max'); //每页加载次数
        $page_size = $spage_size * $spage_max; //每页显示个数
        $pager = $this->_pager($count, $page_size);
        !$field && $field = 'i.id,i.uid,i.uname,i.title,i.intro,i.img,i.price,i.likes,i.comments,i.comments_cache';
        $item_list = $item_tag_mod->field($field)->where($where)->join($db_pre . 'item i ON i.id = ' . $item_tag_table . '.item_id')->order($order)->limit($pager->firstRow . ',' . $spage_size)->select();
        foreach ($item_list as $key => $val) {
            isset($val['comments_cache']) && $item_list[$key]['comment_list'] = unserialize($val['comments_cache']);
        }
        $this->assign('item_list', $item_list);
        //当前页码
        $p = $this->_get('p', 'intval', 1);
        $this->assign('p', $p);
        //当前页面总数大于单次加载数才会执行动态加载
        if (($count - ($p - 1) * $page_size) > $spage_size) {
            $this->assign('show_load', 1);
        }
        //总数大于单页数才显示分页
        $count > $page_size && $this->assign('page_bar', $pager->fshow());
        //最后一页分页处理
        if ((count($item_list) + $page_size * ($p - 1)) == $count) {
            $this->assign('show_page', 1);
        }
    }

    public function cate_ajax() {
        $cid = $this->_get('cid', 'intval');
        //$sort = $this->_get('sort', 'trim', 'pop');
       // $min_price = $this->_get('min_price', 'intval');
        //$max_price = $this->_get('max_price', 'intval');

        //分类数据
        if (false === $cate_data = F('cate_data')) {
            $cate_data = D('item_cate')->cate_data_cache();
        }
        //分类关系
        if (false === $cate_relate = F('cate_relate')) {
            $cate_relate = D('item_cate')->relate_cache();
        }

        //条件
        $where = array();
        //排序：潮流(pop)，最热(hot)，最新(new)
      /*  switch ($sort) {
            case 'pop':
                $order = 'likes DESC';
                break;
            case 'hot':
                $order = 'hits DESC';
                break;
            case 'new':
                $order = 'id DESC';
                break;
        }*/
        if ($cate_data[$cid]['type'] == 0) {
            //实物分类
          //  $min_price && $where['price'][] = array('egt', $min_price);
          //  $max_price && $where['price'][] = array('elt', $max_price); //价格

            array_push($cate_relate[$cid]['sids'], $cid);
            $where['cate_id'] = array('in', $cate_relate[$cid]['sids']); //分类

            $this->wall_ajax($where, $order);
        } else {
            //标签组
         //   $min_price && $where['i.price'][] = array('egt', $min_price);
          //  $max_price && $where['i.price'][] = array('elt', $max_price); //价格

            $db_pre = C('DB_PREFIX');
            $item_tag_table = $db_pre . 'item_tag';
            $tag_ids = M('item_cate_tag')->where(array('cate_id' => $cid))->getField('tag_id', true);
            if ($tag_ids) {
                $where[$item_tag_table . '.tag_id'] = array('IN', $tag_ids);
                $pentity_id = D('item_cate')->get_pentity_id($cid); //向上找实体分类
                array_push($cate_relate[$pentity_id]['sids'], $pentity_id);
                $where['i.cate_id'] = array('IN', $cate_relate[$pentity_id]['sids']); //分类条件
                $this->tcate_wall_ajax($where, 'i.' . $order);
            }
        }
    }

    public function tcate_wall_ajax($where, $order = 'i.id DESC', $field = '') {
        $db_pre = C('DB_PREFIX');
        $item_tag_table = $db_pre . 'item_tag';
        $item_tag_mod = M('item_tag');

        $spage_size = C('pin_wall_spage_size'); //每次加载个数
        $spage_max = C('pin_wall_spage_max'); //加载次数
        $p = $this->_get('p', 'intval', 1); //页码
        $sp = $this->_get('sp', 'intval', 1); //子页
        //条件
        $where_init = array('i.status' => '1');
        $where = array_merge($where_init, $where);
        //计算开始
        $start = $spage_size * ($spage_max * ($p - 1) + $sp);
        $item_mod = M('item');
        $count = $item_tag_mod->where($where)->join($db_pre . 'item i ON i.id = ' . $item_tag_table . '.item_id')->count();
        !$field && $field = 'i.id,i.uid,i.uname,i.title,i.intro,i.img,i.price,i.likes,i.comments,i.comments_cache';
        $item_list = $item_tag_mod->field($field)->where($where)->join($db_pre . 'item i ON i.id = ' . $item_tag_table . '.item_id')->order($order)->limit($start . ',' . $spage_size)->select();
        foreach ($item_list as $key => $val) {
            isset($val['comments_cache']) && $item_list[$key]['comment_list'] = unserialize($val['comments_cache']);
        }
        $this->assign('item_list', $item_list);
        $resp = $this->fetch('public:waterfall');
        $data = array(
            'isfull' => 1,
            'html' => $resp
        );
        $count <= $start + $spage_size && $data['isfull'] = 0;
        $this->ajaxReturn(1, '', $data);
    }

}